package smedi.ismedi.mobile.bff.controller.safetycheck;

import com.smedi.ismedi.authoritymanagement.api.dto.response.RoleBasicResponse;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAuthorityRoleManagementFeignApi;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.inmail.api.dto.request.TaskRecordRequest;
import com.smedi.ismedi.inmail.api.feign.RemoteTaskFeignApi;
import com.smedi.ismedi.safetycheck.dto.request.daily.QueryListSafetyDailyRequest;
import com.smedi.ismedi.safetycheck.dto.request.daily.QuerySafetyDailyRequest;
import com.smedi.ismedi.safetycheck.dto.request.daily.SafetyDailyRequest;
import com.smedi.ismedi.safetycheck.dto.request.danger.QueryHiddenDangerListRequest;
import com.smedi.ismedi.safetycheck.dto.request.inspection.QueryInspectionRecordListRequest;
import com.smedi.ismedi.safetycheck.dto.response.daily.ProjectCalendarResponse;
import com.smedi.ismedi.safetycheck.dto.response.daily.SafetyDailyResponse;
import com.smedi.ismedi.safetycheck.dto.response.danger.HiddenDangerResponse;
import com.smedi.ismedi.safetycheck.dto.response.inspection.InspectionRecordResponse;
import com.smedi.ismedi.safetycheck.feign.RemoteHiddenDangerFeignApi;
import com.smedi.ismedi.safetycheck.feign.RemoteInspectionRecordFeignApi;
import com.smedi.ismedi.safetycheck.feign.RemoteSafetyDailyFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.mobile.bff.controller.BaseController;
import smedi.ismedi.mobile.bff.dto.SafetyDailyDetailResponse;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(tags = {"安全日志接口"})
@RequestMapping("/mobilebff/v1/safetyDaily")
@AllArgsConstructor
public class SafetyDailyController extends BaseController {
    private final DozerUtil dozerUtil;

    private final RemoteSafetyDailyFeignApi remoteSafetyDailyFeignApi;

    private final RemoteInspectionRecordFeignApi remoteInspectionRecordFeignApi;

    private final RemoteHiddenDangerFeignApi remoteHiddenDangerFeignApi;

    private final RemoteAuthorityRoleManagementFeignApi remoteAuthorityRoleManagementFeignApi;

    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;

    private final RemoteTaskFeignApi remoteTaskFeignApi;

    @ApiOperation(value = "安全日志维护")
    @PostMapping(value = "/safetyDaily")
    ResponseData<SafetyDailyResponse> safetyDaily(@RequestBody SafetyDailyRequest safetyDailyRequest) {
        ResponseData<SafetyDailyResponse> responseData;
        if (safetyDailyRequest.getId() != null) {
            responseData = remoteSafetyDailyFeignApi.updateDaily(safetyDailyRequest);
        } else {
            responseData = remoteSafetyDailyFeignApi.addSafetyDaily(safetyDailyRequest);
        }
        if (safetyDailyRequest.getSubmitted() != null && safetyDailyRequest.getSubmitted()) {
            remoteSafetyDailyFeignApi.commitSafetyDaily(responseData.getData().getId());
        }
        return responseData;
    }

    /**
     * 安全日志列表
     *
     * @param queryListSafetyDailyRequest
     * @return
     */
    @ApiOperation(value = "查询安全日志列表")
    @GetMapping(value = "/getSafetyDailyList")
    ResponseData<List<SafetyDailyDetailResponse>> getSafetyDailyList(@Validated @SpringQueryMap QueryListSafetyDailyRequest queryListSafetyDailyRequest) {
        ResponseData<List<SafetyDailyResponse>> responseDataDailyList = remoteSafetyDailyFeignApi.queryDailyList(queryListSafetyDailyRequest);

        List<SafetyDailyResponse> safetyDailyResponseList = responseDataDailyList.getData();

        List<SafetyDailyDetailResponse> safetyDailyDetailResponses = dozerUtil.convertor(safetyDailyResponseList, SafetyDailyDetailResponse.class);

        safetyDailyDetailResponses.stream().forEach(x -> {
            QueryInspectionRecordListRequest queryInspectionRecordListRequest = new QueryInspectionRecordListRequest();
            queryInspectionRecordListRequest.setProjectId(x.getProjectId());
            queryInspectionRecordListRequest.setInspectionDate(x.getDailyDate() == null ? null : x.getDailyDate());
            ResponseData<List<InspectionRecordResponse>> listResponseData = remoteInspectionRecordFeignApi.queryRecordList(queryInspectionRecordListRequest);
            x.setInspectionRecordResponses(listResponseData.getData());

            QueryHiddenDangerListRequest queryHiddenDangerListRequest = new QueryHiddenDangerListRequest();
            queryHiddenDangerListRequest.setProjectId(x.getProjectId());
            queryHiddenDangerListRequest.setCheckDate(x.getDailyDate() == null ? null : x.getDailyDate().toString());
            ResponseData<List<HiddenDangerResponse>> hiddenDangerList = remoteHiddenDangerFeignApi.getHiddenDangerList(queryHiddenDangerListRequest);
            x.setHiddenDangerResponses(hiddenDangerList.getData());
        });

        return new ResponseData<>().success(safetyDailyDetailResponses);
    }

    /**
     * 安全日志明细
     *
     * @param querySafetyDailyRequest
     * @return
     */
    @ApiOperation(value = "查询安全日志")
    @GetMapping(value = "/getSafetyDailyDetail")
    ResponseData<SafetyDailyDetailResponse> getSafetyDailyDetail(@Validated @SpringQueryMap QuerySafetyDailyRequest querySafetyDailyRequest) {
        ResponseData<SafetyDailyResponse> responseData = remoteSafetyDailyFeignApi.queryDaily(querySafetyDailyRequest);

        SafetyDailyResponse safetyDailyResponse = responseData.getData();

        SafetyDailyDetailResponse safetyDailyDetailResponse = dozerUtil.convertor(safetyDailyResponse, SafetyDailyDetailResponse.class);

        if (safetyDailyDetailResponse == null) {
            safetyDailyDetailResponse = new SafetyDailyDetailResponse();
            safetyDailyDetailResponse.setProjectId(querySafetyDailyRequest.getProjectId());
            safetyDailyDetailResponse.setDailyDate(querySafetyDailyRequest.getDailyDate() == null ? null : LocalDate.parse(querySafetyDailyRequest.getDailyDate()));
        }

        // 不存在安全记录时， 默认显示当天的隐患记录 和 打卡记录
        if (safetyDailyDetailResponse.getDailyDate() != null && safetyDailyDetailResponse.getProjectId() != null) {
            QueryInspectionRecordListRequest queryInspectionRecordListRequest = new QueryInspectionRecordListRequest();
            queryInspectionRecordListRequest.setProjectId(safetyDailyDetailResponse.getProjectId());
            queryInspectionRecordListRequest.setInspectionDate(safetyDailyDetailResponse.getDailyDate());
            ResponseData<List<InspectionRecordResponse>> listResponseData = remoteInspectionRecordFeignApi.queryRecordList(queryInspectionRecordListRequest);
            safetyDailyDetailResponse.setInspectionRecordResponses(listResponseData.getData());

            QueryHiddenDangerListRequest queryHiddenDangerListRequest = new QueryHiddenDangerListRequest();
            queryHiddenDangerListRequest.setProjectId(safetyDailyDetailResponse.getProjectId());
            queryHiddenDangerListRequest.setCheckDate(safetyDailyDetailResponse.getDailyDate().toString());
            ResponseData<List<HiddenDangerResponse>> hiddenDangerList = remoteHiddenDangerFeignApi.getHiddenDangerList(queryHiddenDangerListRequest);
            safetyDailyDetailResponse.setHiddenDangerResponses(hiddenDangerList.getData());
        }

        return new ResponseData<>().success(safetyDailyDetailResponse);
    }

    /**
     * 安全日志日历状态
     *
     * @param projectId
     * @return
     */
    @ApiOperation(value = "安全日志日历状态")
    @GetMapping(value = "/queryCalendarStatus")
    ResponseData<ProjectCalendarResponse> queryCalendarStatus(@RequestParam("projectId") Long projectId, @RequestParam(value = "startTime",required = false) String startTime, @RequestParam(value = "endTime",required = false) String endTime) {
        return remoteSafetyDailyFeignApi.queryCalendarStatus(projectId,startTime,endTime);
    }
}
